<template>
  <div class="row col-12">
    <div class="col-6">
      <table class="table table-sm">
        <caption>
          差旅地区分类
        </caption>
        <thead>
          <tr>
            <th>类别</th>
            <th>城市</th>
            <th></th>
          </tr>
        </thead>
        <tr v-for="info of dataList">
          <td>{{ showZoneDesc(info.travelZoneId) }}</td>
          <td>{{ info.cityName }}</td>
          <td>
            <button
              class="btn btn-sm btn-danger"
              @click.stop="deleteZone(info.travelZoneId, info.cityId)"
            >
              删除
            </button>
          </td>
        </tr>
      </table>
    </div>
    <div class="col-6 bg-light">
      <form class="form-horizontal">
        <div class="row mb-2">
          <label class="control-label col-3 text-end"
            ><strong>类别</strong></label
          >
          <div class="col-6">
            <select
              class="form-select"
              v-model.number="zoneId"
            >
              <option value="1">一类地区</option>
              <option value="2">二类地区</option>
            </select>
          </div>
        </div>
        <div class="row mb-2">
          <label class="control-label col-3 text-end"
            ><strong>城市</strong></label
          >
          <div class="col-6">
            <select-city :cityId.sync="cityId"></select-city>
          </div>
        </div>

        <div class="row">
          <button
            id="btnChange"
            type="button"
            class="btn btn-primary  col-4 offset-4"
            @click.stop="saveZone()"
          >
            保存
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import {
  searchCustomerTravelZones,
  searchCustomerFlightDiscounts,
  saveCustomerTravelZone,
} from "@/api/customer.js";

import MyModalPrompt from "@/components/modal/my-modal-prompt.vue";
import SelectCity from "@/components/my-select2-city.vue";

export default {
  props: {
    customerId: {
      type: Number,
    },
  },
  components: {
    MyModalPrompt,
    SelectCity,
  },
  data() {
    return {
      modalTitle: "",
      saving: false,

      dataList: [],

      cityId: 0,
      zoneId: 1,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  activated: function () {
    consle.log("custome-travel-zone: activated");
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    search: function () {
      const params = {
        "sc.pageNo": 1,
        "sc.pageSize": 1000,
      };
      searchCustomerTravelZones(this.customerId, params, (v) => {
        this.dataList = v.dataList;
      });
    },
    saveZone: function () {
      const params = {
        zoneId: this.zoneId,
        cityId: this.cityId,
        mode: 0,
      };

      // const jsonParam = JSON.stringify(params)

      saveCustomerTravelZone(this.customerId, params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.search();
          if (this.keepOpening === false) {
            this.reset();
          }
        } else {
          this.showErrMsg("保存失败：" + v.errmsg, "danger");
        }
      });
    },
    deleteZone: function (zoneId, cityId) {
      const params = {
        zoneId: zoneId,
        cityId: cityId,
        mode: 4,
      };
      saveCustomerTravelZone(this.customerId, params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败：" + v.errmsg, "danger");
        }
      });
    },
    showZoneDesc: function (zoneId) {
      switch (zoneId) {
        case 1:
          return "一类地区";
        case 2:
          return "二类地址";
        default:
          return "其它地区";
      }
    },
  },
};
</script>